<?php
/**
 * 标签
 * 
 * @copyright 	Copyright (c) 2011 thinkapk.com All rights reserved.
 * @package 	ThinkAPK_Action
 * @link 		http://www.thinkapk.com/
 * @author 		richie <richbossman@163.com>
 * @version 	$Id: TagAction.class.php 7482 2011-05-19 10:12:51Z richie $
 * 
 */
class TagAction extends ThinkAPK_Action_Web {
	
	public $tags='';
	private $options = array();
	private $grade = '';
	private $sort = array(0=>'g.add_time', 1=>'gc.down_total', 2=>'gc.good');
	private $sortText = array(0=>'最新更新', 1=>'下载最多', 2=>'评分最高');
	private $limit = 10;
	
	function _before_index(){
		$this->setOptions();
		//排序
   		$this->assign('sortOrder', $this->setSort());
   		//tag
   		$this->assign('tags', $this->tags);
   		//
   		$total = $this->getAppTotal();
   		$list = $this->getAppList();
   		$pageshow = P($total, $this->limit, '', array('url'=>$this->setUrl(array('page'=>'%page%'))));
		$this->assign('pageshow', $pageshow);
		$this->assign('total', $total);
		$this->assign('app_list', $list);
		$this->assign('options', $this->options);
		$this->assign('cates', D('Category')->get(0, 'App'));
		//tag入库
		$isOldTag = M('Tags')->where(array('tags'=>$this->tags))->find();
		if($isOldTag){
			M('Tags')->where(array('id'=>$isOldTag['id']))->save(array('total'=>$total, 'click'=>array('exp', 'click+1')));
		}else{
			M('Tags')->add(array('tags'=>$this->tags, 'total'=>$total, 'click'=>1));
		}
   		//页面属性
   		$this->setTitle($this->tags.'相关的Android应用或游戏');
   		$this->setPosition(0, '搜索 “'.$this->tags.'”，相似结果：'.$total);
   		$this->setKeywords('Android,安卓,'.$this->tags);
   		$this->setDescription('以下是创想安卓显示的与“'.$this->tags.'”有关的Android软件与游戏。');
   		unset($total, $list, $pageshow);
   		//print_r($this->setUrl());
	}
	
	//应用列表
	private function getAppList(){
		$order = $this->sort[$this->options['dsc']].' DESC';
		$map = $this->getConditionForFitler();
		$row = D()->table(C('DB_PREFIX').'goods AS g')
			->field('g.goods_id as id, gc.down_total, gc.good, gc.bad, g.goods_name as title, g.short_title, g.add_time,  g.goods_icon as icon, g.recommend, g.short_desc as gdesc, g.goods_brief, g.short_desc')
			->where($map)
			->join(C('DB_PREFIX').'goods_count as gc ON gc.goods_id=g.goods_id')
			->group('g.goods_id')
			->limit($this->limit)
			->page($this->options['page'])
			->order($order)
			->findAll();
		if($row){
			foreach($row as $key=>$app){
				$row[$key]['url'] = U('thinkapk://Android/index', array('id'=>$app['id']));
				$row[$key]['short_title'] = !empty($app['short_title'])?$app['short_title']:$app['title'];
				$row[$key]['icon'] = $this->getIconPath($app['icon']);
				$row[$key]['time'] =  $this->formatTime($app['add_time'], true);
				unset($row[$key]['goods_name'], $app);
			}
		}
		unset($map);
		return $row;
	}
	
	//应用统计
	private function getAppTotal(){
		$map = $this->getConditionForFitler();
		$row = D()->table(C('DB_PREFIX').'goods AS g')->where($map)->count();
		return $row;
	}
	
	//取得查询筛选条件所需要的查询条件
	private function getConditionForFitler(){
		$map = " g.is_delete=0 AND g.is_on_sale=1 AND (g.goods_name LIKE '%$this->tags%' OR g.short_title LIKE '%$this->tags%' OR g.keywords LIKE '%$this->tags%')";
		return $map;
	}
	
	//设置排序
	private function setSort(){
		$row = $this->sort;
		foreach ($row as $k=>$st){
			unset($row[$k]);
			$row[$k]['url'] = $this->setUrl(array('dsc'=>$k));
			$row[$k]['name'] = $this->sortText[$k];
			$row[$k]['sort'] = $st;
		}
		return $row;
	}
	
	//设置参数
	private function setOptions(){
		$this->tags = isset($_GET['tags'])?urldecode(trim($_GET['tags'])):'';
		$this->grade = isset($_REQUEST['grade']) ? trim($_REQUEST['grade']) : '';
		$this->options['dsc'] = $this->getParemsValue('dsc', '0');
		$this->options['page'] = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
	}
	
	//取得参数值
	private function getParemsValue($name, $default='0'){
		$value=array();
		preg_match("/$name(.*)-/isU", $this->grade.'-', $value);
		return isset($value[1]) ? $value[1] : $default;
	}
	
	//设置url
	private function setUrl($options=array()){
		$option = array(
			'page' => $this->options['page'],
			'id' => urlencode($this->tags),
			'dsc' => $this->options['dsc']
		);
		$option = array_merge($option, $options);
		return U('thinkapk://'.MODULE_NAME.'/index', $option);
	}
}
?>